<?php

namespace app\admin\controller\wadviser;

use app\common\controller\Backend;
use app\common\model\AdviserArticleType as ArticleTypeModel;

/**
 * 谋士管理
 *
 * @icon fa fa-user-secret
 */
class Articletype extends Backend
{
    protected $model = null;
    protected $searchFields = ['title'];
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->assignconfig("adviser_id", input('adviser_id'));
        $this->model = new ArticleTypeModel;
    }
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['adviser'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array(
                "total" => $list->total(),
                "rows" => $list->items()
            );
            return json($result);
        }
        return $this->view->fetch();
    }
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if ($params) {
                $params['adviser_id'] = input('adviser_id');
                $result = $this->model->save($params);
                if ($result !== false) {
                    $this->success();
                }
                $this->error($this->model->getError());
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }
    
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if ($params) {
                $result = $row->save($params);
                if ($result !== false) {
                    $this->success();
                }
                $this->error($row->getError());
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
}